<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\SettingDocument
 *
 * @property int $id
 * @property int $type 类型(1 注册,2 提单 3 收款 4 收款)
 * @property string $channel_code 资方编号
 * @property string $document_name 文档名称
 * @property string $document_url 文档的路径
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingDocument newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingDocument newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingDocument query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingDocument whereChannelCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingDocument whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingDocument whereDocumentName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingDocument whereDocumentUrl($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingDocument whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingDocument whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingDocument whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property string $document_content 文档的的内容 是一个json
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingDocument whereDocumentContent($value)
 * @property string $url_set 点击的url设置
 * @property int $is_use_url 是否启用url,1启用0不启用
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingDocument whereIsUseUrl($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SettingDocument whereUrlSet($value)
 */
class SettingDocument extends Model
{

    const REGISTER = 1;//注册协议
    const LOAN = 2;//进件-授信协议
    const RECEIPT_ONE = 3;//收款-合同协议
    const RECEIPT_TWO = 4;//收款-保险协议
    //是否使用url
    const USE_URL_FALSE = 0;
    const USE_URL_TRUE = 1;

    //类型对应的默认名称
    static $DOCUMENT_LIST = [
        self::REGISTER => '快闪卡贷注册服务协议',
        self::LOAN => '个人信息查询书',
        self::RECEIPT_ONE => '贷款服务相关合同',
        self::RECEIPT_TWO => '借款人综合保险'
    ];

    //
    protected $guarded = ['id'];

    public static function getDocumentList(){
        $documentType = self::documentType();
        $setting_chan = new SettingChannel();
        $channelList = $setting_chan->pluck('channel_name','channel_code');
        //判断资方有没有添加到特定类型中
        foreach ($documentType as $key => $val){
            if($val['is_channel_code']){
                if($channelList){
                    foreach ($channelList as $k => $v){
                        //判断当前数据库
                        $insertData = array();
                        $insertData['type'] = $key;
                        $insertData['channel_code'] = $k;
                        self::firstOrCreate($insertData,['document_content' => json_encode([])]);
                    }
                }
            }else{
                self::firstOrCreate(['type'=>$key],['document_content' => json_encode([])]);
            }
        }

        $info = (new SettingDocument())->setConnection('backend')->select('id','type','channel_code','document_name','document_content','is_use_url')
            ->orderBy('type','asc')
            ->get();

        //获取资方列表
        $info = $info->toArray();
        foreach ($info as $k => &$v){
            if(empty($documentType[$v['type']])){
               //删除这个多余的类型数据
                self::where('id',$v['id'])->delete();
                unset($info[$k]);
                continue;
            }else{
                $type = $documentType[$v['type']];
                $v['msg'] = $type['msg'];
            }
            if($documentType[$v['type']]['is_channel_code']){
                //资方如果删除了
                if(empty($channelList[$v['channel_code']])){
                    //删除这个多余的类型数据
                    self::where('id',$v['id'])->delete();
                    unset($info[$k]);
                    continue;
                }else{
                    $v['channel_name'] = $channelList[$v['channel_code']];

                }
            }else{
                //is_channel_code变为false,删除code的

            }
        }

        $info = array_values($info);

        return $info;
    }

    /**
     * Description:app通过类型获取文件
     * Author:lijiafei
     * @return array
     */
    public static function getDocumentByType($type = '',$channel_code = ''){
        $where = array();
        if(!empty($type)){
            $where[] = ['type','=',$type];
        }
        $list = (new SettingDocument())->setConnection('backend')->select('type','channel_code','document_name','document_content','url_set','is_use_url')
            ->where($where)
            ->get()->toArray();
        $documentType = self::documentType();
        $returnData = array();
        foreach ($list as $k => $v){
            if(!empty($documentType[$v['type']]))
            {

                //注册协议不管后台是否配置都要使用编辑的内容
                if($v['type'] == self::REGISTER){
                    $v['is_use_url'] = self::USE_URL_FALSE;
                }

                $v['msg'] = $documentType[$v['type']]['msg'];
                $v['document_content'] = json_decode($v['document_content'],true);
                if($documentType[$v['type']]['is_channel_code']){
                    //true,需要通过授权信息获取数据
                    if($channel_code == $v['channel_code']){
                        unset($v['channel_code']);
                        $returnData[] = $v;
                    }
                }else{
                    unset($v['channel_code']);
                    $returnData[] = $v;
                }
            }
        }
        return $returnData;
    }

    /**
     * 获取收款1 和收款2 的记录信息
     */
    public static function getReceipt($channel_code){
        if(is_null($channel_code))return false;
        $info = self::whereIn('type',[self::RECEIPT_ONE,self::RECEIPT_TWO])->select('type','document_name') -> where('channel_code',$channel_code) -> get();
        return $info->toArray();
    }

    public static function documentType(){
        return $data = [
            self::REGISTER => ['msg' => '注册协议','is_channel_code' => false,'url_set' => ''],
            self::LOAN => ['msg' => '进件-授信协议','is_channel_code' => false,'url_set' => 'https://nfsp-manager.nflow.cn/agreement?type=CREDIT'],
            self::RECEIPT_ONE => ['msg' => '收款-合同协议','is_channel_code' => true,'url_set' => 'https://nfsp-manager.nflow.cn/a/agreement?id=1547450169400obEppkrz'],
            self::RECEIPT_TWO => ['msg' => '收款-保险协议','is_channel_code' => true,'url_set' => 'https://nfsp-manager.nflow.cn/agreement?type=INSURANCE'],
        ];
    }

    public static function getBeforeApply()
    {
        
    }
}